package com.mxl.rabbitmq.controller;

import com.alibaba.fastjson.JSON;
import com.mxl.rabbitmq.config.*;
import com.mxl.rabbitmq.model.MessageBody;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.UUID;

/**
 * @author MXL
 * @date 2021/9/14 0014
 **/
@RestController
public class SendMsgController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 发送到Simple-Queue, 该队列绑定的是死信交换机
     *
     * @return
     */
    @RequestMapping("/sendSimpleMsg")
    public String sendSimpleMsg() throws InterruptedException {
        MessageBody messageBody = new MessageBody(200, "Simple消息-测试", null, System.currentTimeMillis(), 99);
        //延迟消息第2种方法: 队列设置延迟时间
        Message message = MessageBuilder.withBody("Simple消息-测试".getBytes())
                .build();
        rabbitTemplate.convertAndSend(/*TTLConfig.TTL_EXCHANGE_NAME, */DlxConfig.SIMPLE_QUEUE_NAME, message/*, new CorrelationData(UUID.randomUUID().toString())*/);
        return "发送Simple消息成功!" + LocalTime.now();
    }

    /**
     * 发送到Simple-Queue, 该队列绑定的是死信交换机
     *
     * @return
     */
    @RequestMapping("/sendDlxMsg")
    public String sendDlxMsg() throws InterruptedException {
        MessageBody messageBody = new MessageBody(200, "延迟消息-测试", null, System.currentTimeMillis(), 99);
        //延迟消息第2种方法: 队列设置延迟时间
        Message message = MessageBuilder.withBody("死信消息-测试".getBytes())
                .build();
        String msg = "HelloWorld rabbitmq";
        for (Integer i = 0; i < 10; i++) {
            CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
            rabbitTemplate.convertAndSend(DlxConfig.SIMPLE_EXCHANGE_NAME, DlxConfig.SIMPLE_ROUTING_KEY, msg + i, correlationData);
        }

        //rabbitTemplate.convertAndSend(/*TTLConfig.TTL_EXCHANGE_NAME, */DlxConfig.DLX_QUEUE_NAME, message/*, new CorrelationData(UUID.randomUUID().toString())*/);
        return "发送Dlx消息成功!" + LocalTime.now();
    }

    /**
     * 延迟10s发送消息, 给消息设置 延迟时间
     *
     * @return
     */
    @RequestMapping("/sendTTLMsg")
    public String sendTTLMsg() throws InterruptedException {
        MessageBody messageBody = new MessageBody(200, "延迟消息-测试", null, System.currentTimeMillis(), 99);
        //延迟消息第1种方法: 发送消息时设置 过期时间
        /*Message message = MessageBuilder.withBody("延迟消息-测试".getBytes())
                .setExpiration(String.valueOf(1000 * 20))
                .build();*/

        //延迟消息第2种方法: 队列设置延迟时间
        Message message = MessageBuilder.withBody("延迟消息-测试".getBytes())
                .build();
        rabbitTemplate.convertAndSend(/*TTLConfig.TTL_EXCHANGE_NAME, */TTLConfig.TTL_QUEUE_NAME, message/*, new CorrelationData(UUID.randomUUID().toString())*/);
        return "发送TTL消息成功!" + LocalTime.now();
    }

    /**
     * 发送 Direct 消息
     *
     * @return
     */
    @RequestMapping("/sendJavaBoyMsg")
    public String sendJavaBoyMsg() throws InterruptedException {

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryTag(123);
        Message message = new Message("JavaBoy-Direct内容".getBytes(), messageProperties);
        for (int i = 0; i < 20; i++) {
            MessageBody messageBody = new MessageBody(200, "JavaBoy-Direct内容-" + i, null, System.currentTimeMillis(), i);
            rabbitTemplate.convertAndSend(JavaBoyConfig.JAVABOY_EXCHANGE_NAME, JavaBoyConfig.JAVABOY_QUEUE_NAME, messageBody, new CorrelationData(UUID.randomUUID().toString()));
        }
        return "发送JavaBoy-direct消息成功!";
    }

    /**
     * 发送 Direct 消息
     *
     * @return
     */
    @RequestMapping("/sendDirectMsg")
    public String sendDirectMsg() {
        MessageBody messageBody = new MessageBody(200, "Direct内容", LocalDateTime.now().toString());
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend(DirectConfig.QUEUE_NAME_DIRECT, messageBody);
        return "发送direct消息成功!";
    }

    /**
     * 发送Fanout 消息
     *
     * @return
     */
    @GetMapping("/sendFanoutMsg")
    public String sendFanoutMsg() {
        MessageBody messageBody = new MessageBody(300, "fanput内容", LocalDateTime.now().toString());
        rabbitTemplate.convertAndSend(FanoutConfig.EXCHANGE_NAME_FANOUT, "", messageBody);
        return "发送fanout消息成功!";
    }

    /**
     * 发送Topic 消息
     *
     * @return
     */
    @GetMapping("/sendTopicMsgByRoutingKey")
    public String sendTopicMsg(String routingKey) {
        MessageBody messageBody = new MessageBody(300, "topic内容", LocalDateTime.now().toString());
        rabbitTemplate.convertAndSend(TopicConfig.EXCHANGE_NAME_TOPIC, routingKey, messageBody);
        return "发送topic消息成功!";
    }

    /**
     * 发送 延迟消息
     *
     * @return
     */
    @GetMapping("/sendDelayedMsg")
    public String sendDelayedMsg() {
        MessageBody messageBody = new MessageBody(600, "sendDelayedMsg", LocalDateTime.now().toString());
        MessageProperties mp = new MessageProperties();
        // 设置过期时间
        mp.setDelay(5 * 1000);
        Message message = new Message(JSON.toJSONString(messageBody).getBytes(), mp);
        rabbitTemplate.convertAndSend(DelayedConfig.EXCHANGE_NAME_DELAYED, "DELAY.MSG", message);
        return "发送delayed消息成功!";
    }

    @GetMapping("/sendTopicMessage2")
    public String sendTopicMessage2() {
        MessageBody messageBody = new MessageBody(400, "sendTopicMessage2", LocalDateTime.now().toString());
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", messageBody);
        return "ok";
    }

    /**
     * 测试exchange不存在, 所走的回调
     *
     * @return
     */
    @GetMapping("/exchangeIsNotExist")
    public String exchangeIsNotExist() {
        MessageBody messageBody = new MessageBody(500, "TestMessageAck", LocalDateTime.now().toString());
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", messageBody);
        return "测试exchange不存在, 所走的回调";
    }

    /**
     * 测试queue不存在, 所走的回调
     *
     * @return
     */
    @GetMapping("/queueIsNotExist")
    public String queueIsNotExist() {
        MessageBody messageBody = new MessageBody(600, "TestMessageAck2", LocalDateTime.now().toString());
        rabbitTemplate.convertAndSend(JavaBoyConfig.JAVABOY_EXCHANGE_NAME, "non-existent-queue", messageBody);
        return "测试queue不存在, 所走的回调";
    }


}
